﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Reborn_WorldServer;

namespace App1AppHandler
{
    public class SpellManager
    {
        public SpellPage[] SpellPages = new SpellPage[11];

        private Character myCharacter;

        public SpellManager(Character character) 
        {
            myCharacter = character;

            //LOAD ITEM IDs
            SqlConnection MyCon = new SqlConnection(Managers.DBConnectionString);
            SqlCommand SQLCom = new SqlCommand("Characters_Spells_Select", MyCon);
            SQLCom.CommandType = System.Data.CommandType.StoredProcedure;
            SQLCom.Parameters.AddWithValue("@CharacterID", character.ID);

            MyCon.Open();
            SqlDataReader SDR = SQLCom.ExecuteReader();

            List<SpellPage> PagesToUpdate = new List<SpellPage>();
            while (SDR.Read())
            {
                SpellPage spellPage = new SpellPage();
                spellPage.PageNumber = int.Parse(SDR["PageNumber"].ToString());
                //WALK THROUGH COLUMNS
                for (int x = 0; x < 12; x++)
                {
                    if (SDR["Item" + x.ToString()] != DBNull.Value)
                    {
                        SpellIcon spellIcon = character.GameHandler.Spells[int.Parse(SDR["Item" + x.ToString()].ToString())];
                        spellPage.SpellIcons[x] = spellIcon;
                    }
                }
                SpellPages[spellPage.PageNumber] = spellPage;
                PagesToUpdate.Add(spellPage);
            }

            SDR.Close();
            MyCon.Close();
            MyCon.Dispose();

            character.AddSpellPages(PagesToUpdate);
        }

        public bool slotExists(int spellPage, int index)
        {
            if (SpellPages[spellPage] == null)
                return true;
            if (SpellPages[spellPage].SpellIcons[index] == null)
                return true;

            return false;
        }

        public bool moveSpellIcon(int sourceSpellPage, int sourceIndex, int destSpellPage, int destIndex)
        {
            if (slotExists(destSpellPage, destIndex) == false)
                return false;

            if (SpellPages[sourceSpellPage] == null)
                return false;
            if (SpellPages[sourceSpellPage].SpellIcons[sourceIndex] == null)
                return false;

            SpellIcon iconToMove = SpellPages[sourceSpellPage].SpellIcons[sourceIndex];
            SpellPages[sourceSpellPage].SpellIcons[sourceIndex] = null;

            if (SpellPages[destSpellPage] == null)
            {
                SpellPages[destSpellPage] = new SpellPage();
                SpellPages[destSpellPage].PageNumber = destSpellPage;
            }
            SpellPages[destSpellPage].SpellIcons[destIndex] = iconToMove;

            if (sourceSpellPage == destSpellPage)
                myCharacter.AddSpellPages(SpellPages[sourceSpellPage]);
            else
            {
                List<SpellPage> update = new List<SpellPage>();
                update.Add(SpellPages[sourceSpellPage]);
                update.Add(SpellPages[destSpellPage]);
                myCharacter.AddSpellPages(update);
            }

            return true;
        }
        
        public void SaveSpells()
        {
            SqlConnection MyCon = new SqlConnection(Managers.DBConnectionString);
            MyCon.Open();
            SqlTransaction MyTrans = MyCon.BeginTransaction();

            SqlCommand SQLCom = new SqlCommand();
            SQLCom.CommandType = System.Data.CommandType.StoredProcedure;
            SQLCom.Connection = MyCon;
            SQLCom.Transaction = MyTrans;

            try
            {
                SQLCom.CommandText = "Characters_Spells_DeletePerCharacterID";
                SQLCom.Parameters.AddWithValue("@CharacterID", myCharacter.ID);
                SQLCom.ExecuteNonQuery();

                SQLCom.Parameters.Clear();
                SQLCom.CommandText = "Characters_Spells_Update";
                for (int x = 0; x < SpellPages.Length; x++)
                {
                    SQLCom.Parameters.AddWithValue("@CharacterID", myCharacter.ID);
                    SQLCom.Parameters.AddWithValue("@PageNumber", x);

                    if (SpellPages[x] != null)
                    {
                        for (int y = 0; y < SpellPages[x].SpellIcons.Length; y++)
                        {
                            if (SpellPages[x].SpellIcons[y] != null)
                            {
                                SQLCom.Parameters.AddWithValue("@Item" + y.ToString(), SpellPages[x].SpellIcons[y].ID_DB);
                            }
                        }
                    }
                    SQLCom.ExecuteNonQuery();
                    SQLCom.Parameters.Clear();
                }
                MyTrans.Commit();
            }
            catch
            {
                MyTrans.Rollback();
            }
            finally
            {
                MyCon.Close();
                MyCon.Dispose();
            }
        }
    }
}
